<?php

namespace App\Console\Commands;
use App\Models\ArticleRelateCircle;
use App\Models\Circle;
use App\Models\Subscribe;
use App\Models\UserExtend;
use App\Models\UserJoinCircle;
use App\Services\PublishTemplateService;
use Illuminate\Console\Command;

class SignInNoticeCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sign-in-notice';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '签到提醒';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Subscribe::where('type', Subscribe::TYPE_SIGN_IN)->withCertain('user', ['openid'])->chunkById(50, function ($list) {
            foreach ($list as $item) {
                $series_day = UserExtend::withCertain('user', ['openid'])->where('user_id', $item->user_id)->where('last_sign_date', '<', date('Y-m-d'))->value('series_day');
                $service = new PublishTemplateService();
                if ($series_day) {
                    $service->signIn($item->user->openid, $series_day);
                    $item->delete();
                }

            }
        });
    }
}
